package com.bree.erp.jsf.validator;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import com.bree.erp.entity.User;
import com.bree.erp.service.UserService;
import com.bree.erp.service.util.ServiceUtil;

@ManagedBean(name="passwordValidator")
public class ExistingPasswordValidator  {

	@EJB
	private UserService userService;
	
	private UIInput login;
	private UIInput currentPassword;
	private UIInput newPassword;
	private UIInput retypePassword;
	

	public void validate(FacesContext context, UIComponent arg1, Object object)
			throws ValidatorException {

		String email = login.getValue().toString();

		String password = object.toString();
		String encodedPassword = ServiceUtil.encodePassword(password);

		User user = userService.read(email);

		if (!encodedPassword.equals(user.getPassword())) {
			FacesMessage msg = new FacesMessage("Passowrd validation failed.",
					"Existing password does not matched!");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
		}

	}
	
	public void validateNew(FacesContext context, UIComponent arg1, Object object)
			throws ValidatorException {
		
		String currPassword = currentPassword.getValue().toString();
		
		String newPass = object.toString();

		if (currPassword.equals(newPass)) {
			FacesMessage msg = new FacesMessage("Password validation failed.",
					"Please enter new password!");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
		}

	}

	public void validateRetype(FacesContext context, UIComponent arg1, Object object)
			throws ValidatorException {

		String newPass = this.newPassword.getValue().toString();
		String retypePass = object.toString();
		
		if (!retypePass.equals(newPass)) {
			FacesMessage msg = new FacesMessage("Password validation failed.",
					"New password doesn't matched!");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
		}

	}

	
	/**
	 * @return the login
	 */
	public UIInput getLogin() {
		return login;
	}


	/**
	 * @param login the login to set
	 */
	public void setLogin(UIInput login) {
		this.login = login;
	}


	/**
	 * @return the currentPassword
	 */
	public UIInput getCurrentPassword() {
		return currentPassword;
	}


	/**
	 * @param currentPassword the currentPassword to set
	 */
	public void setCurrentPassword(UIInput currentPassword) {
		this.currentPassword = currentPassword;
	}


	/**
	 * @return the newPassword
	 */
	public UIInput getNewPassword() {
		return newPassword;
	}


	/**
	 * @param newPassword the newPassword to set
	 */
	public void setNewPassword(UIInput newPassword) {
		this.newPassword = newPassword;
	}


	/**
	 * @return the retypePassword
	 */
	public UIInput getRetypePassword() {
		return retypePassword;
	}


	/**
	 * @param retypePassword the retypePassword to set
	 */
	public void setRetypePassword(UIInput retypePassword) {
		this.retypePassword = retypePassword;
	}

}
